package util

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/storage"
	"fyne.io/fyne/v2/widget"
)

// go build -o myapp.exe main.go
func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("Hello World")

	// 创建组件
	statusLabel := widget.NewLabel("Hello Fyne!")
	button := widget.NewButton("点击我",
		func() {
			fileDialog := dialog.NewFileOpen(func(reader fyne.URIReadCloser, err error) {
				if err != nil {
					statusLabel.SetText("错误: " + err.Error())
					return
				}
				if reader == nil {
					return
				}
				defer reader.Close()

				// 读取CSV文件
				filePath := reader.URI().Path()
				_, err = readCSVLineByLine(filePath)
				if err != nil {
					statusLabel.SetText("读取错误: " + err.Error())
					return
				}

			}, myWindow)

			// 设置文件过滤器
			fileDialog.SetFilter(storage.NewExtensionFileFilter([]string{".csv"}))
			fileDialog.Show()
		},
	)

	// 布局
	content := container.NewVBox(
		statusLabel,
		button,
	)

	myWindow.SetContent(content)
	myWindow.Resize(fyne.NewSize(400, 300))
	myWindow.ShowAndRun()
}
